//
//  DetailViewController.m
//  Example
//
//  Created by Andney Jack on 2020/6/2.
//  Copyright © 2020 annidy. All rights reserved.
//

#import "DetailViewController.h"
#import <CTSuperPlayer.h>
#import <UIImageView+WebCache.h>
#import "BaseNavigationController.h"

// 随机图片地址
NSString * RandomImgUrl(void)
{
    return [NSString stringWithFormat:@"https://meiriyiwen.com/images/new_feed/bg_%d.jpg", arc4random() % 98 + 1];
}

@interface DetailViewController () <CTSuperPlayerDelegate>
{
    BOOL _allowRotate;
    BOOL _statusBarHidden;
}
@property (nonatomic, strong) UIView *playerContainer;
@property (nonatomic, strong) CTSuperPlayerView *playerView;
@property (nonatomic, strong) UIButton *playButton;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [self.actionTitles addObject:@"返回"];

    [super viewDidLoad];

    self.navigationItem.title = @"详情页";

    [self loadUI];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.playerView resetPlayer];
}

- (void)loadUI
{
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectZero];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.layer.masksToBounds = YES;
    imgView.userInteractionEnabled = YES;
    [imgView sd_setImageWithURL:[NSURL URLWithString:RandomImgUrl()]];
    [self.view addSubview:imgView];

    self.playerContainer = [UIView new];
    self.playerContainer.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    [imgView addSubview:self.playerContainer];

    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.playButton setImage:[UIImage imageNamed:@"icon_goods_video_play"] forState:UIControlStateNormal];
    [self.playButton addTarget:self action:@selector(playButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [imgView addSubview:self.playButton];

    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_topMargin);
        make.height.equalTo(imgView.mas_width).multipliedBy(9.0 / 16);
    }];

    [self.playerContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(imgView);
    }];

    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(imgView);
        make.size.mas_equalTo(80);
    }];
}

- (void)handleAction:(NSString *)action
{
    if ([action isEqualToString:@"返回"]) {
        [self closePage:YES];
    }
}

- (void)playButtonClick:(UIButton *)sender {
    if (!_playerView) {
        _playerView = [[CTSuperPlayerView alloc] init];
        // 设置父View
        _playerView.disableGesture = YES;
//        _playerView.rotateOption = CTPlayerRotateOption_RotateScreen;
//        _playerView.hideBackButtonForCompactStyle = YES;
//        _playerView.disableAllControlView = YES;

        self.playerView.fatherView = self.playerContainer;
        self.playerView.delegate = self;
    }

    CTSuperPlayerModel *playerModel = [[CTSuperPlayerModel alloc] init];
    playerModel.videoURL = self.url ? : @"http://1253131631.vod2.myqcloud.com/26f327f9vodgzp1253131631/f4c0c9e59031868222924048327/f0.mp4";

    [_playerView playWithModel:playerModel];

    self.playButton.hidden = YES;
}

#pragma mark - —————————————— CTSuperPlayerDelegate ————————————

/// 返回事件
- (void)superPlayerBackAction:(CTSuperPlayerView *)player
{
    [self.playerView resetPlayer];
    [self closePage:YES];
}

/// 全屏改变通知
- (void)superPlayerFullScreenChanged:(CTSuperPlayerView *)player
{
    if (player.isFullScreen) {
        _statusBarHidden = YES;
    } else {
        _statusBarHidden = NO;
    }

    [self setNeedsStatusBarAppearanceUpdate];
}

/// 播放开始通知
- (void)superPlayerDidStart:(CTSuperPlayerView *)player
{
}

/// 播放结束通知
- (void)superPlayerDidEnd:(CTSuperPlayerView *)player
{
}

/// 播放错误通知
- (void)superPlayerDidError:(CTSuperPlayerView *)player errorCode:(NSInteger)errorCode errorMessage:(NSString *)errorMessage
{
}

- (void)superPlayerShouldRotate:(CTSuperPlayerView *)player canRotate:(BOOL)canRotate {
    _allowRotate = canRotate;

    if ([self.navigationController isKindOfClass:[BaseNavigationController class]]) {
        BaseNavigationController *nav = (BaseNavigationController *)self.navigationController;
        nav.allowRotate = canRotate;
    }

    [self setNeedsStatusBarAppearanceUpdate];
}

#pragma mark - —————————————— 屏幕旋转 ————————————

- (BOOL)prefersStatusBarHidden {
    return _statusBarHidden;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.playerView) {
        if (self.playerView.isFullScreen) {
            return UIStatusBarStyleLightContent;
        }
    }
    return UIStatusBarStyleDefault;
}

//- (BOOL)shouldAutorotate {
//    return _allowRotate;
//}
//
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    if (_allowRotate) {
//        return UIInterfaceOrientationMaskAllButUpsideDown;
//    } else {
//        if (self.playerView.isFullScreen) {
//            return UIInterfaceOrientationMaskLandscape;
//        } else {
//            return UIInterfaceOrientationMaskPortrait;
//        }
//    }
//}

@end
